package com.bulletelc.apollopre.module.display.product.resultList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.framework.MVPBaseActivity;
import com.bulletelc.apollopre.module.display.product.resultList.fragment.ResultListFragment;
import com.bulletelc.apollopre.module.display.search.SearchActivity;
import com.bulletelc.apollopre.widget.FoolSortNav;

/**
 * Created by CX on 2018/4/12.
 */

public class ResultListActivity extends MVPBaseActivity<ResultListContact.View, ResultListPresenter> implements ResultListContact.View
    ,View.OnClickListener, FoolSortNav.onNavClickCallback {

    // views
    private LinearLayout llBack;
    private FoolSortNav fsnMain; // 自定义TabNav
    private TextView tvSearch; // 搜索
    private LinearLayout llColumn; //布局按钮
    private ImageView ivLayoutType; // 布局Icon

    // data
    public String keyword;
    public String classifyId;
    private FragmentManager fragmentManager;
    private ResultListFragment priceSortListFragment;
    private ResultListFragment recommendListFragment;
    private ResultListFragment saleSortListFragment;
    private ResultListFragment hotSortListFragment;
    private static boolean LAYOUT_CHANGE_TYPE = true; // 布局切换，true为水平布局，false为锤子布局;

    // 重用Activity,重写回调方法
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        getData();
        initFragment();
    }

    @Override
    public int getLayoutRes() {
        return R.layout.result_list_activity;
    }

    @Override
    public void assignViews() {
        llBack = findView(R.id.llBack);
        llColumn = findView(R.id.llColumn);
        fsnMain = findView(R.id.fsnMain);
        tvSearch = findView(R.id.tvSearch);
        ivLayoutType = findView(R.id.ivLayoutType);
    }
    @Override
    public void setListener() {
        llBack.setOnClickListener(this);
        llColumn.setOnClickListener(this);
        tvSearch.setOnClickListener(this);
        fsnMain.setNavClickCallback(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        getData();
        initFragment();
    }
    @Override
    protected ResultListPresenter createPresenter() {
        return new ResultListPresenter();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.llBack:
                finish();
                break;
            case R.id.tvSearch:
                startActivity(new Intent(this, SearchActivity.class));
                break;
            case R.id.llColumn: // 切换视图列表
                switchLayout();
                break;
        }
    }

    /**
     * 切换布局
     */
    private void switchLayout() {
        if (LAYOUT_CHANGE_TYPE) {
            LAYOUT_CHANGE_TYPE = false;
            ivLayoutType.setBackgroundResource(R.drawable.sel_column);
        } else {
            LAYOUT_CHANGE_TYPE = true;
            ivLayoutType.setBackgroundResource(R.drawable.sel_grid);
        }
        recommendListFragment.change(LAYOUT_CHANGE_TYPE);
        priceSortListFragment.change(LAYOUT_CHANGE_TYPE);
        saleSortListFragment.change(LAYOUT_CHANGE_TYPE);
    }

    private void initFragment() {
        priceSortListFragment = new ResultListFragment(); // 价格
        recommendListFragment = new ResultListFragment(); // 推荐
        saleSortListFragment = new ResultListFragment(); // 销量
        hotSortListFragment = new ResultListFragment(); // 热销

        Bundle bundle = new Bundle();
        bundle.putString(IntentCode.EXTRA_KEY_SEARCH_KEYWORD, keyword);
        bundle.putString(IntentCode.EXTRA_KEY_CLASSIFY_ID, classifyId);


        Bundle bundleForPriceFragment = new Bundle();
        bundleForPriceFragment.putString(IntentCode.EXTRA_KEY_SEARCH_KEYWORD, keyword);
        bundleForPriceFragment.putString(IntentCode.EXTRA_KEY_CLASSIFY_ID, classifyId);
        bundleForPriceFragment.putString(IntentCode.EXTRA_SORT_PRODUCT_SORT_FIELD, "sales_price");

        Bundle bundleForSaleFragment = new Bundle();
        bundleForSaleFragment.putString(IntentCode.EXTRA_KEY_SEARCH_KEYWORD, keyword);
        bundleForSaleFragment.putString(IntentCode.EXTRA_KEY_CLASSIFY_ID, classifyId);
        bundleForSaleFragment.putString(IntentCode.EXTRA_SORT_PRODUCT_SORT_FIELD, "-sold");

        priceSortListFragment.setArguments(bundleForPriceFragment);
        recommendListFragment.setArguments(bundle);
        saleSortListFragment.setArguments(bundleForSaleFragment);
        hotSortListFragment.setArguments(bundle);

        fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();

        ft.add(R.id.flMain, priceSortListFragment);
        ft.add(R.id.flMain, recommendListFragment);
        ft.add(R.id.flMain, saleSortListFragment);
        ft.add(R.id.flMain, hotSortListFragment);
        ft.commit();

        showFragment(priceSortListFragment);
    }

    @Override
    public void onClickRecommendSort() {
        showFragment(recommendListFragment);
    }

    @Override
    public void onClickHotSort() {
        showFragment(recommendListFragment);
    }

    @Override
    public void onClickPriceSortAsc() {
        showFragment(priceSortListFragment);
        priceSortListFragment.sortPrice("lowest_price");
    }

    @Override
    public void onClickPriceSortDesc() {
        showFragment(priceSortListFragment);
        priceSortListFragment.sortPrice("-lowest_price");
    }

    @Override
    public void onClickSaleSort() {
        showFragment(saleSortListFragment);
    }

    @Override
    public void showFragment() {
        showFragment(priceSortListFragment);
    }

    /**
     * 显示fragment
     *
     * @param fragment
     */
    private void showFragment(Fragment fragment) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.hide(recommendListFragment);
        ft.hide(priceSortListFragment);
        ft.hide(saleSortListFragment);
        ft.hide(hotSortListFragment);
        ft.show(fragment);
        ft.commit();
    }

    /**
     * 获取intent参数
     */
    private void getData() {
        keyword = null;
        classifyId = null;
        Intent intent = getIntent();
        if (intent != null) {
            keyword = intent.getStringExtra(IntentCode.EXTRA_KEY_SEARCH_KEYWORD);
            classifyId = intent.getStringExtra(IntentCode.EXTRA_KEY_CLASSIFY_ID);
        }
    }

}
